using Core;
using UnityEditor;
using UnityEngine;

namespace Editor.Game
{
    public partial class GameEditor : EditorWindow
    {
        private Controller controller;

        private Vector2 gameViewScrollValue = Vector2.zero;

        [MenuItem("Mini Tools/Editor/Game Editor", false)]
        private static void Open()
        {
            var window = GetWindow<GameEditor>("Game Editor", true);
            window.minSize = new Vector2(1400f, 600f);
        }

        private void OnEnable()
        {
            controller = new Controller();

            controller.Load();
        }

        private void OnGUI()
        {
            EditorGUILayout.BeginHorizontal(GUILayout.Width(position.width), GUILayout.Height(position.height));
            {
                GUILayout.Space(2f);
                EditorGUILayout.BeginVertical(GUILayout.Width(position.width * 0.25f));
                {
                    GUILayout.Space(5f);
                    EditorGUILayout.LabelField(Utils.Text.Format("Game List ({0})", controller.Count), EditorStyles.boldLabel);
                    EditorGUILayout.BeginHorizontal("box", GUILayout.Height(position.height - 68f));
                    {
                        DrawGameView();
                    }
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Space(5f);
                        // DrawResourcesMenu();
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.EndVertical();
                EditorGUILayout.BeginVertical(GUILayout.Width(position.width * 0.35f));
                {
                    GUILayout.Space(5f);
                    EditorGUILayout.LabelField(Utils.Text.Format("Resource Content ({0})", 0), EditorStyles.boldLabel);
                    EditorGUILayout.BeginHorizontal("box", GUILayout.Height(position.height - 52f));
                    {
                        // DrawResourceView();
                    }
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Space(5f);
                        // DrawResourceMenu();
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.EndVertical();
                EditorGUILayout.BeginVertical(GUILayout.Width(position.width * 0.4f - 16f));
                {
                    GUILayout.Space(5f);
                    EditorGUILayout.LabelField("Asset List", EditorStyles.boldLabel);
                    EditorGUILayout.BeginHorizontal("box", GUILayout.Height(position.height - 52f));
                    {
                        // DrawSourceAssetsView();
                    }
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Space(5f);
                        // if (m_AssetListState == MenuState.Normal)
                        // {
                        //     DrawSourceAssetsMenu();
                        // }
                        // else
                        // {
                        //     DrawSearchField();
                        // }
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.EndVertical();
                GUILayout.Space(5f);
            }
            EditorGUILayout.EndHorizontal();
        }


        private void DrawGameView()
        {
            // m_CurrentResourceRowOnDraw = 0;
            gameViewScrollValue = EditorGUILayout.BeginScrollView(gameViewScrollValue);
            {
                var gameTbl = controller.TblGame;
                var confs = gameTbl.Confs;
                for (var i = 0; i < confs.Length; i++)
                {
                    DrawResourceFolder(confs[i]);
                }
            }
            EditorGUILayout.EndScrollView();
        }

        private void DrawResourceFolder(TblGame.Conf conf)
        {
            EditorGUILayout.BeginHorizontal();
            {
// #if UNITY_2019_3_OR_NEWER
//                 bool foldout = EditorGUI.Foldout(new Rect(18f + 14f * folder.Depth, 20f * m_CurrentResourceRowOnDraw + 4f, int.MaxValue, 14f), expand, string.Empty, true);
// #else
//                 bool foldout = EditorGUI.Foldout(new Rect(18f + 14f * folder.Depth, 20f * m_CurrentResourceRowOnDraw + 2f, int.MaxValue, 14f), expand, string.Empty, true);
// #endif
//                 if (expand != foldout)
//                 {
//                     expand = !expand;
//                     SetExpandedResourceFolder(folder, expand);
//                 }
                // GUI.DrawTexture(new Rect(32f + 14f , 20f  + 3f, 16f, 16f), ResourceFolder.Icon);
                EditorGUILayout.LabelField("ASD", GUILayout.Width(44f + 14f), GUILayout.Height(18f));

// #if UNITY_2019_3_OR_NEWER
//                 GUI.DrawTexture(new Rect(32f + 14f * folder.Depth, 20f * m_CurrentResourceRowOnDraw + 3f, 16f, 16f), ResourceFolder.Icon);
//                 EditorGUILayout.LabelField(string.Empty, GUILayout.Width(44f + 14f * folder.Depth), GUILayout.Height(18f));
// #else
//                 GUI.DrawTexture(new Rect(32f + 14f * folder.Depth, 20f * m_CurrentResourceRowOnDraw + 1f, 16f, 16f), ResourceFolder.Icon);
//                 EditorGUILayout.LabelField(string.Empty, GUILayout.Width(40f + 14f * folder.Depth), GUILayout.Height(18f));
// #endif
                EditorGUILayout.LabelField(conf.Desc);
            }
            EditorGUILayout.EndHorizontal();

            // m_CurrentResourceRowOnDraw++;
            //
            // if (expand)
            // {
            //     foreach (ResourceFolder subFolder in folder.GetFolders())
            //     {
            //         DrawResourceFolder(subFolder);
            //     }
            //
            //     foreach (ResourceItem resourceItem in folder.GetItems())
            //     {
            //         DrawResourceItem(resourceItem);
            //     }
            // }
        }
    }
}